import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.Calculations;
import org.rsbot.script.Constants;
import org.rsbot.script.Script;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.wrappers.RSArea;
import org.rsbot.script.wrappers.RSCharacter;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSItemTile;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSTile;

/**
 * 
 * MysticGSpiderAFKer
 * 
 * @link http://www.rsbot.org/vb/showthread.php?t=57981
 * 
 * @Author Mouchicc < mourad94_4@hotmail.com >
 * 
 * @Date 2009
 * 
 * @param Updates
 *            : None
 * @version 1.0
 * 
 * 
 */
@ScriptManifest(authors = { "Mouchicc" }, category = "Combat", name = "MGSpiders", version = 1.7, description = "<html>"
		+ "<head>"
		+ "<style type=\"text/css\"> hr {color: white} p {margin-left: 20px}</style>"
		+ "</head>"
		+ "<body>"
		+ "<center>"
		+ "<b><font size=\"6\" color=\"red\">"
		+ "MGSpiders"
		+ " v"
		+ "1"
		+ " M"
		+ "</font></b><br>"
		+ "<font size=\"3\" color=\"red\">Please start @ the eastern Crabs.<br></font>"
		+ "<center><table border=\"0\"><tr><td colspan=\"5\"><center><font size=6><b>== Options and Settings ==</b></font></center></td></tr><tr><td colspan=4><hr></td></tr><tbody><tr><td width=\"135\">"
		+ "<b>Use Special:</b></td><td width=\"140\"><center><input type=\"checkbox\" name=\"spec\" value=\"true\"></center></td><td width=\"135\"></b></center></td></tr>"
		+ "<b><td>Use Potions:</b></td><td width=\"140\"><center><input type=\"checkbox\" name=\"pot\" value=\"true\"></center></td><td width=\"135\"></b></center></td></tr>"
		+ "<b><tr><td>Arrow Pickup:</b></td><td width=\"140\"><center><input type=\"checkbox\" name=\"ranged\" value=\"true\"></center></td><td width=\"135\"></b></center></td></tr></tr>"
		+ "<tr><td><b>Eat At:</b>&nbsp<input type=\"text\" name=\"eatt\" size=\"4\"</center></td></tr>"
		+ "<tr><td><b>Food ID:</b>&nbsp<input type=\"text\" name=\"foodid\" size=\"4\"</center></td></tr>"
		+ "<b><td>Attack Style:</b></td><td width=\"140\"><center><select name=\"skill\"><option>Strength<option>Attack<option>Defence<option>Rapid Ranged<option>Accurate Ranged<option>Longrange Ranged</select></center></td><td width=\"135\"></b></center></td></tr>"
		+ "</center>" + "</center></body>" + "</html>")
public class MGSpiders extends Script implements PaintListener {

	ScriptManifest properties = getClass().getAnnotation(ScriptManifest.class);
	BufferedImage MCSeries = null;

	// ***Doors***\\
	private final RSTile[] GATE1 = { new RSTile(2149, 5302),
			new RSTile(2148, 5302) };
	private final RSTile[] GATE2 = { new RSTile(2149, 5299),
			new RSTile(2148, 5299) };

	// ***North-East and South-West Tiles*** \\
	public RSTile northEastSpiders = new RSTile(2149, 5307);
	public RSTile southWestSpiders = new RSTile(2148, 5302);

	public RSTile northEastBDoors = new RSTile(2149, 5301);
	public RSTile southWestBDoors = new RSTile(2148, 5299);

	public RSTile northEastODoors = new RSTile(2149, 5298);
	public RSTile southWestODoors = new RSTile(2148, 5296);

	// ***RSAreas*** \\
	RSArea betweenDoors = new RSArea(southWestBDoors, northEastBDoors);
	RSArea atSpiders = new RSArea(southWestSpiders, northEastSpiders);
	RSArea outsideDoors = new RSArea(southWestODoors, northEastODoors);

	RSTile[] getAggr = { new RSTile(2162, 5301), new RSTile(2172, 5306) };
	RSTile atCatablepons = new RSTile(2172, 5306);

	// ***Ints and longs***\\
	int HP;
	int foodID;
	int levelsGained;
	int curStatExp;
	int startLvl;
	int startExp;
	int stat;
	int startxp;
	long lvls = 0;
	int startLVL;
	int CurSTAT;
	int RealSTAT;
	long startTime = System.currentTimeMillis();
	public int ITWID1 = 0000;
	public int ITWID2 = 0000;
	public int[] ITWIDs;
	public static int[] potID;
	int eatAT;
	public int fightMode;
	int runEnergy = random(25, 60);
	public static int[] FoodID = { 1895, 1893, 1891, 4293, 2142, 291, 2140,
			3228, 9980, 7223, 6297, 6293, 6295, 6299, 7521, 9988, 7228, 2878,
			7568, 2343, 1861, 13433, 315, 325, 319, 3144, 347, 355, 333, 339,
			351, 329, 3381, 361, 379, 380, 10136, 5003, 379, 365, 373, 374,
			7946, 385, 397, 391, 3369, 3371, 3373, 2309, 2325, 2333, 2327,
			2331, 2323, 2335, 7178, 7180, 7188, 7190, 7198, 7200, 7208, 7210,
			7218, 7220, 2003, 2011, 2289, 2291, 2293, 2295, 2297, 2299, 2301,
			2303, 1891, 1893, 1895, 1897, 1898, 1899, 1900, 1901, 1902, 7072,
			7062, 7078, 7064, 7084, 7082, 7066, 7068, 1942, 6701, 6703, 7054,
			6705, 7056, 7060, 2130, 1985, 1993, 1989, 1978, 5763, 5765, 1913,
			5747, 1905, 5739, 1909, 5743, 1907, 1911, 5745, 2955, 5749, 5751,
			5753, 5755, 5757, 5759, 5761, 2084, 2034, 2048, 2036, 2217, 2213,
			2205, 2209, 2054, 2040, 2080, 2277, 2225, 2255, 2221, 2253, 2219,
			2281, 2227, 2223, 2191, 2233, 2092, 2032, 2074, 2030, 2281, 2235,
			2064, 2028, 2187, 2185, 2229, 6883, 1971, 4608, 1883, 1885 };

	// ***Other*** \\
	String skill;
	String state;
	RSNPC GSpider = getNearestNPCByID(4400);
	public boolean doingspec = false;
	public boolean potion = false;
	public boolean attackSpiders = false;
	public boolean AFKSpiders = false;
	public boolean ranged = false;
	String status = "";

	String description = "<html>"
			+ "<head>"
			+ "<style type=\"text/css\"> hr {color: white} p {margin-left: 20px}</style>"
			+ "</head>"
			+ "<body>"
			+ "<center>"
			+ "<b><font size=\"6\" color=\"red\">"
			+ "MGSpiders"
			+ " v"
			+ "1"
			+ " M"
			+ "</font></b><br>"
			+ "<font size=\"3\" color=\"red\">Please start @ the eastern Crabs.<br></font>"
			+ "<center><table border=\"0\"><tr><td colspan=\"5\"><center><font size=6><b>== Options and Settings ==</b></font></center></td></tr><tr><td colspan=4><hr></td></tr><tbody><tr><td width=\"135\">"
			+ "<b>Use Special:</b></td><td width=\"140\"><center><input type=\"checkbox\" name=\"spec\" value=\"true\"></center></td><td width=\"135\"></b></center></td></tr>"
			+ "<b><td>Use Potions:</b></td><td width=\"140\"><center><input type=\"checkbox\" name=\"pot\" value=\"true\"></center></td><td width=\"135\"></b></center></td></tr>"
			+ "<b><tr><td>Arrow Pickup:</b></td><td width=\"140\"><center><input type=\"checkbox\" name=\"ranged\" value=\"true\"></center></td><td width=\"135\"></b></center></td></tr></tr>"
			+ "<tr><td><b>Eat At:</b>&nbsp<input type=\"text\" name=\"eatt\" size=\"4\"</center></td></tr>"
			+ "<tr><td><b>Food ID:</b>&nbsp<input type=\"text\" name=\"foodid\" size=\"4\"</center></td></tr>"
			+ "<b><td>Attack Style:</b></td><td width=\"140\"><center><select name=\"skill\"><option>Strength<option>Attack<option>Defence<option>Rapid Ranged<option>Accurate Ranged<option>Longrange Ranged</select></center></td><td width=\"135\"></b></center></td></tr>"
			+ "</center>" + "</center></body>" + "</html>";

	// What to load on Start
	@Override
	public boolean onStart(Map<String, String> args) {
		log("************************************************");
		log("********MysticGSpiderAFKer V1" + " started!********");
		log("*********Let's get some Experience**************");
		log("************************************************");
		startLVL = skills.getCurrentSkillLevel(stat);
		if (args.get("ranged") != null) {
			ranged = true;
		} else {
			ranged = false;
		}
		if (args.get("spec") != null) {
			doingspec = true;
		} else {
			doingspec = false;
		}
		if (args.get("pot") != null) {
			potion = true;
		} else {
			potion = false;
		}
		if (args.get("skill").equals("Attack")) {
			stat = STAT_ATTACK;
			skill = "Attack";
			fightMode = 0;
			potID = new int[] { 121, 122, 123, 124, 125, 126, 145, 146, 147,
					148, 149, 150, 2428, 2429, 2436, 2437, 11429, 11430, 11431,
					11432, 9739, 9740, 9741, 9742, 9743, 9744, 9745, 9746 };
		}
		if (args.get("skill").equals("Strength")) {
			stat = STAT_STRENGTH;
			skill = "Strength";
			fightMode = 1;
			potID = new int[] { 113, 114, 115, 116, 117, 118, 119, 120, 157,
					158, 159, 160, 161, 162, 2440, 2441, 11441, 11442, 11443,
					11444, 9739, 9740, 9741, 9742, 9743, 9744, 9745, 9746 };

		}
		if (args.get("skill").equals("Defence")) {
			stat = STAT_DEFENSE;
			skill = "Defence";
			fightMode = 3;
			potID = new int[] { 133, 134, 135, 136, 137, 138, 163, 164, 165,
					166, 167, 168, 2432, 2433, 2442, 2443, 11457, 11458, 11459,
					11460, 9739, 9740, 9741, 9742, 9743, 9744, 9745, 9746 };
		}
		if (args.get("skill").equals("Accurate Ranged")) {
			stat = STAT_RANGE;
			skill = "Ranged";
			fightMode = 0;
			potID = new int[] { 169, 170, 171, 172, 173, 174, 2444, 2445,
					11509, 11510, 11511, 11512 };
		}
		if (args.get("skill").equals("Rapid Ranged")) {
			stat = STAT_RANGE;
			skill = "Ranged";
			fightMode = 1;
			potID = new int[] { 169, 170, 171, 172, 173, 174, 2444, 2445,
					11509, 11510, 11511, 11512 };
		}
		if (args.get("skill").equals("Longrange Ranged")) {
			stat = STAT_RANGE;
			skill = "Ranged";
			fightMode = 3;
			potID = new int[] { 169, 170, 171, 172, 173, 174, 2444, 2445,
					11509, 11510, 11511, 11512 };
		}
		if (args.get("eatt").isEmpty()) {
			eatAT = (skills.getCurrentSkillLevel(3) / 100) * random(25, 33);
		} else if (!args.get("eatt").equals("")) {
			eatAT = Integer.parseInt(args.get("eatt"));
		}
		/*
		 * try { URL MySeries = new URL(
		 * "http://i480.photobucket.com/albums/rr163/AlpinePhotos/mcseries-3.gif"
		 * ); MCSeries = ImageIO.read(MySeries); } catch (IOException e) { }
		 */
		return true;
	}

	// What to do when finished
	public void onFinish() {
		log("Thanks for using the script!");
		log("XP per hour: "
				+ ((skills.getCurrentSkillExp(stat) - startExp) * (3600000D / (((double) System
						.currentTimeMillis() - (double) startTime)))));
		log("Levels Gained: " + lvls);
		log("Experience Gained: "
				+ (skills.getCurrentSkillExp(stat) - startExp));
	}

	// Drinks potion
	public void drinkIt() {
		CurSTAT = skills.getCurrentSkillLevel(stat);
		RealSTAT = skills.getRealSkillLevel(stat);
		if (CurSTAT - RealSTAT <= random(1, 2) && potion == true) {
			for (int i = 0; i < potID.length; i++) {
				if (getInventoryCount(potID[i]) >= 1) {
					clickInventoryItem(potID[i], "Drink");
					status = "Drinking pot..";
					return;
				}
			}
		} else {
			wait(random(20, 30));
			return;
		}
	}

	// Gets Special
	public int getSpec() {
		return getSetting(300);
	}

	// Gets if Special is Enabled
	public boolean specEnabled() {
		return (getSetting(301)) == 1;
	}

	// Does Special!
	public void doSpec() {
		if (getSpec() >= 1000 && !specEnabled() && doingspec) {
			openTab(Constants.TAB_ATTACK);
			clickMouse(645 + random(0, 4), 425 + random(0, 4), true);
			wait(350);
		}
		return;
	}

	public void eat() {
		for (int i = 0; i < FoodID.length; i++) {
			if (getInventoryCount(FoodID[i]) >= 1) {
				if (clickInventoryItem(FoodID[i], "Eat")) {
					log("Normal");
				} else {
					clickInventoryItem(FoodID, "Eat");
					log("Other");
				}
			}
		}

	}

	// Gets east to make Spiders aggresive again
	public void GET_AGGRESIVEAGAIN() {

		if (atArea(atSpiders)) {
			getThatDoor(GATE1, "Open", "North");
			wait(random(100, 200));
		} else if (atArea(betweenDoors)) {
			getThatDoor(GATE2, "Open", "North");
			wait(random(100, 200));
		}

		if (atArea(outsideDoors)) {
			if (!getMyPlayer().isMoving() || distanceTo(getDestination()) < 7) {
				walkPathMM(randomizePath(getAggr, 2, 2), 17);
			}
		}
	}

	// Gets back at the Spiders
	public void GET_BACK() {

		if (distanceTo(GATE2[random(0, 1)]) <= 3 && atArea(outsideDoors)) {
			getThatDoor(GATE2, "Open", "North");
		} else if (atArea(betweenDoors)) {
			getThatDoor(GATE1, "Open", "North");
			wait(random(100, 400));
		}
	}

	public void getAggresive() {
		RSNPC GSpider = getNearestNPCByID(4400);
		if (distanceTo(GSpider) <= 2 && !atArea(betweenDoors)) {
			getThatDoor(GATE1, "Open", "North");
			wait(random(100, 200));
		}

		if (atArea(betweenDoors) && !atArea(outsideDoors)) {
			getThatDoor(GATE2, "Open", "North");
			wait(random(100, 200));
		}

		if (atArea(outsideDoors) && !atArea(betweenDoors)) {
			walkPathMM(randomizePath(getAggr, 2, 2), 17);
		}
	}

	public void getBack() {
		if (distanceTo(GATE2[random(0, 1)]) <= 3 || atArea(outsideDoors)) {
			getThatDoor(GATE2, "Open", "North");
		}

		if (atArea(betweenDoors) && !atArea(outsideDoors)) {
			getThatDoor(GATE1, "Open", "North");
			wait(random(100, 400));
		}
	}

	@Override
	public int loop() {
		if (getMyPlayer().isMoving()) {
			return 800;
		}

		if (getFightMode() != fightMode) {
			setFightMode(fightMode);
		}

		if (getEnergy() >= runEnergy) {
			if (!isRunning()) {
				setRun(true);
			}
		}

		if (doingspec) {
			doSpec();
		}

		HP = skills.getCurrentSkillLevel(STAT_HITPOINTS);
		if (HP <= eatAT) {
			eat();
		}

		if (potion) {
			CurSTAT = skills.getCurrentSkillLevel(stat);
			RealSTAT = skills.getRealSkillLevel(stat);
			if (CurSTAT - RealSTAT <= random(1, 2)) {
				for (int i = 0; i < potID.length; i++) {
					if (getInventoryCount(potID[i]) >= 1) {
						clickInventoryItem(potID[i], "Drink");
						status = "Drinking pot..";
						return random(400, 500);
					}
				}
			}
		}

		if (distanceTo(getNearestFreeNPCByID(4400)) <= 1
				&& !getMyPlayer().isInCombat()
				&& getNearestFreeNPCByID(4400) != null
				&& getMyPlayer().getAnimation() == -1
				&& getMyPlayer().getInteracting() == null
				&& !getMyPlayer().isMoving()) {
			wait(random(5000, 7500));
			status = "Getting AGGR";
		} else if (distanceTo(atCatablepons) <= 3) {
			if (!getMyPlayer().isMoving() || distanceTo(getDestination()) < 7) {
				walkPathMM(randomizePath(reversePath(getAggr), 2, 2), 17);
			}
			status = "Getting Back";
			wait(random(2000, 3000));
		}

		if (status == "Getting AGGR") {
			getAggresive();
		}

		if (status == "Getting Back") {
			getBack();
		}

		return random(50, 100);
	}

	// Checks if we are in an area
	public boolean atArea(RSArea area) {
		if (area.contains(getMyPlayer().getLocation())) {
			return true;
		} else {
			return false;
		}
	}

	// Picks up arrows by the ones in your invent
	public int pickArrows() {
		for (RSItemTile t : getGroundItemsByID(getEquipmentArray()[13])) {
			RSCharacter interacting = getMyPlayer().getInteracting();
			if (interacting != null) {
				if (interacting instanceof RSNPC) {
					RSNPC inter = (RSNPC) interacting;
					if (inter.getHPPercent() == 0) {
						clickTile(t, "Take "
								+ IDtoName(getEquipmentArray()[13]));
						wait(random(300, 800));
						return random(50, 100);
					}
				}
			}
		}
		return random(10, 50);
	}

	// Method by DDM for items
	public ArrayList<RSItemTile> getGroundItemsByID(int... ids) {
		ArrayList<RSItemTile> itemsList = new ArrayList<RSItemTile>();
		final int pX = getMyPlayer().getLocation().getX();
		final int pY = getMyPlayer().getLocation().getY();
		final int minX = pX - 35;
		final int minY = pY - 35;
		final int maxX = pX + 35;
		final int maxY = pY + 35;
		for (int x = minX; x <= maxX; x++) {
			for (int y = minY; y <= maxY; y++) {
				final RSItemTile[] items = getGroundItemsAt(x, y);
				for (final RSItemTile item : items) {
					final int iId = item.getItem().getID();
					for (final int id : ids) {
						if (iId == id) {
							itemsList.add(item);
						}
					}
				}
			}
		}
		return itemsList;
	}

	// Gets and opens the door by
	public boolean getThatDoor(RSTile g[], String action, String dir) {
		try {
			Point p = g[random(0, 1)].getScreenLocation();
			RSTile t = new RSTile(p.x, p.y);
			if (pointOnScreen(p)) {
				wait(random(100, 200));
				if (dir == "West") {
					setCompass('w');
				} else if (dir == "East") {
					setCompass('e');
				} else if (dir == "South") {
					setCompass('s');
				} else if (dir == "North") {
					setCompass('n');
				}
				if (clickTile(t, action)) {
					while (getMyPlayer().getAnimation() == 4683
							|| getMyPlayer().getAnimation() == 4682) {
						wait(random(200, 400));
					}
					return true;
				}
			}
			if (p.x == -1 || p.y == -1)
				return false;
			clickMouse(p, 2, 2, true);
			while (getMyPlayer().getAnimation() == 4683
					|| getMyPlayer().getAnimation() == 4682) {
				wait(random(200, 400));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

	// Method by Ruski for good NPC clicking
	@SuppressWarnings("unused")
	private boolean clickNPC(RSNPC npc, String action) {
		if (npc == null) {
			return false;
		}
		RSTile tile = npc.getLocation();
		if (!tile.isValid()) {
			return false;
		}

		try {
			Point screenLoc = npc.getScreenLocation();
			if (distanceTo(tile) > 6 || !pointOnScreen(screenLoc)) {
				turnToTile(tile);
			}
			if (distanceTo(tile) > 20) {
				walkTileMM(tile);
				return false;
			}
			for (int i = 0; i < 12; i++) {
				screenLoc = npc.getScreenLocation();
				if (!npc.isValid() || !pointOnScreen(screenLoc)) {
					return false;
				}
				moveMouse(screenLoc, 5, 5);
				if (getMenuItems().get(0).toLowerCase().contains(
						npc.getName().toLowerCase())) {
					break;
				}
				if (getMouseLocation().equals(screenLoc)) {
					break;
				}
			}
			List<String> menuItems = getMenuItems();
			if (menuItems.isEmpty()) {
				return false;
			}
			for (int a = 0; a < menuItems.size(); a++) {
				if (menuItems.get(a).toLowerCase().contains(
						npc.getName().toLowerCase())) {
					if (menuItems.get(0).toLowerCase().contains(
							action.toLowerCase())) {
						clickMouse(true);
						return true;
					} else {
						clickMouse(false);
						return atMenu(action);
					}
				}
			}
		} catch (Exception e) {
			System.out.print("clickNPC(RSNPC, String) error: " + e);
			return false;
		}
		return false;
	}

	// Paint of Script, Credits to Noobielul
	public void onRepaint(Graphics g) {
		if (isLoggedIn()) {
			if (startTime == 0) {
				startTime = System.currentTimeMillis();
			}

			if (startLvl <= 0 || startExp <= 0) {
				startLvl = skills.getCurrentSkillLevel(stat);
				startExp = skills.getCurrentSkillExp(stat);
			}

			levelsGained = skills.getCurrentSkillLevel(stat) - startLvl;

			long millis = System.currentTimeMillis() - startTime;
			long hours = millis / (1000 * 60 * 60);
			millis -= hours * (1000 * 60 * 60);
			long minutes = millis / (1000 * 60);
			millis -= minutes * (1000 * 60);
			long seconds = millis / 1000;

			int x = 0;
			int y = 20;
			g.setColor(new Color(0, 0, 0, 175));
			g.fillRoundRect(0, 20, 220, 120, 10, 10);
			g.fillRect(4, 319, 502, 17);
			g.setFont(new Font("Papyrus", Font.BOLD, 11));
			g.setColor(Color.YELLOW);
			g.drawString("MysticGSpiderAFKer - V1", x + 10, y += 15);
			g.setColor(Color.ORANGE);
			g.drawString("Bussy for " + hours + ":" + minutes + ":" + seconds,
					x + 10, y += 15);
			g.drawString("Current " + skill + " level : "
					+ skills.getRealSkillLevel(stat), x + 10, y += 15);
			g.drawString("Gained " + skill + " levels : " + levelsGained,
					x + 10, y += 15);
			g.drawString("Gained " + skill + " exp : "
					+ (skills.getCurrentSkillExp(stat) - startExp), x + 10,
					y += 15);
			g.drawString("Status : " + status, x + 10, y += 15);
			g
					.drawString(
							"Exp/hour : "
									+ (int) ((skills.getCurrentSkillExp(stat) - startExp) * (3600000D / (((double) System
											.currentTimeMillis() - (double) startTime)))),
							x + 10, y += 15);
			g.drawImage(MCSeries, 55, 360, null);
			g.setColor(Color.ORANGE);
			g.fillRect(5, 320, 5 * skills.getPercentToNextLevel(stat), 15);
			g.setColor(Color.WHITE);
			g.drawString(skills.getPercentToNextLevel(stat) + "%", 250, 331);
		}
	}

	// Method for clicking an inventoryItem
	public boolean clickInventoryItem(int itemID, String option) {
		if (getCurrentTab() != TAB_INVENTORY) {
			openTab(TAB_INVENTORY);
		}
		int[] items = getInventoryArray();
		java.util.List<Integer> possible = new ArrayList<Integer>();
		for (int i = 0; i < items.length; i++) {
			if (items[i] == itemID) {
				possible.add(i);
			}
		}
		if (possible.size() == 0) {
			return false;
		}
		int idx = possible.get(random(0, possible.size()));
		Point t = getInventoryItemPoint(idx);
		moveMouse(t, 5, 5);
		long waitTime = System.currentTimeMillis() + random(50, 250);
		boolean found = false;
		while (!found && System.currentTimeMillis() < waitTime) {
			wait(15);
			if ((getMenuItems()).get(0).toLowerCase().contains(
					option.toLowerCase())) {
				found = true;
			}
		}
		if (found) {
			clickMouse(true);
			wait(random(150, 250));
			return true;
		}
		clickMouse(false);
		wait(random(150, 250));
		return atMenu(option);
	}

	// clickTile method by DDM
	public boolean clickTile(RSTile tile, String option) {
		Point p = Calculations.tileToScreen(tile);
		p.x += random(-5, 6);
		p.y += random(-5, 6);
		if (p.x < 0 || p.y < 0) {
			return false;
		}

		moveMouse(p);
		// wait(random(100,150));
		getMenuItems();
		@SuppressWarnings("unused")
		List<String> itemsList = getMenuItems();

		long waitTime = System.currentTimeMillis() + random(50, 250);
		boolean foundOption = false;
		while (System.currentTimeMillis() < waitTime && !foundOption) {
			wait(15);
			foundOption = (itemsList = getMenuItems()).get(0).toLowerCase()
					.contains(option.toLowerCase());
		}
		if (foundOption) {
			clickMouse(true);
			wait(random(150, 300));
			return true;
		}
		clickMouse(false);
		wait(random(150, 300));
		return atMenu(option);
	}

	public String IDtoName(int id) {
		try {
			URL url = new URL("http://itemdb-rs.runescape.com/viewitem.ws?obj="
					+ id);
			final BufferedReader reader = new BufferedReader(
					new InputStreamReader(url.openStream()));

			String line;
			int i = 0;
			while ((line = reader.readLine()) != null) {
				if (line.equals("<div class=" + '"' + "subsectionHeader" + '"'
						+ ">")) {
					i++;
					continue;
				}
				if (i == 1) {
					return line;
				}
			}
		} catch (Exception e) {

		}
		return null;
	}

	private boolean clickInventoryItem(int[] ids, String command) {
		try {
			if (getCurrentTab() != TAB_INVENTORY
					&& !RSInterface.getInterface(INTERFACE_BANK).isValid()
					&& !RSInterface.getInterface(INTERFACE_STORE).isValid()) {
				openTab(TAB_INVENTORY);
			}
			int[] items = getInventoryArray();
			java.util.List<Integer> possible = new ArrayList<Integer>();
			for (int i = 0; i < items.length; i++) {
				for (int item : ids) {
					if (items[i] == item) {
						possible.add(i);
					}
				}
			}
			if (possible.size() == 0) {
				return false;
			}
			int idx = possible.get(random(0, possible.size()));
			Point t = getInventoryItemPoint(idx);
			moveMouse(t, 5, 5);
			wait(random(100, 290));
			if (getMenuActions().get(0).equals(command)) {
				clickMouse(true);
				return true;
			} else {
				clickMouse(false);
				return atMenu(command);
			}
		} catch (Exception e) {
			System.out.print("clickInventoryFood(int...) error: " + e);
			return false;
		}
	}
}